package com.lrkj.web.phone;

import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.framework.security.MiniContextUtils;
import com.lrkj.property.domain.Room;
import com.lrkj.waitress.domain.CustomInfo;
import com.lrkj.waitress.domain.Repairs;
import com.lrkj.waitress.service.IRepairsService;
import com.lrkj.waitress.vo.RepairsInVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author huxy
 * @version V1.0
 * @创建时间
 * @备注 投诉/建议
 * @类名
 * @节点
 */
@RestController
@RequestMapping("/phone/complaint")
public class ComplaintAdviseController extends CommonUseBaseController {


    @Autowired
    private IRepairsService repairsService;

    /**
     * @author huxy
     * @创建时间 2022/8/16 2:21 PM
     * @备注 投诉/建议
     * @节点
     */
    @PostMapping("add")
    public AjaxResult complaint(@Valid @RequestBody RepairsInVo repairsInVo) {
        Room room = getRoom(repairsInVo.getRoomId());
        if (room == null) {
            return AjaxResult.error("房间信息不存在！");
        }
        //获取客户信息
        CustomInfo customInfo=getCustomInfo(room.getCommunityId());
        if(customInfo == null){
            return AjaxResult.error("未绑定房屋信息！请先绑定");
        }

        Repairs repairs = new Repairs();
        BeanUtils.copyProperties(repairsInVo, repairs);
        //todo test  测试
        String phoneNum = MiniContextUtils.getPhoneNum();
        repairs.setRepairsPerson(customInfo.getCustomName());
        repairs.setRepairsPhone(phoneNum); //投诉人电话
        repairs.setCreateBy(customInfo.getCustomName());
        repairs.setServiceType("wx");
        repairs.setUnitId(room.getUnitId());
        return AjaxResult.success(repairsService.insertRepairs(repairs));
    }

    /**
     * @author huxy
     * @创建时间 2022/8/16 2:22 PM
     * @备注 我的投诉/建议
     * @节点
     */
    @GetMapping("list")
    public TableDataInfo myComplaint(String roomId) {
        TableDataInfo tableDataInfo;
        Room room = getRoom(roomId);
        if (room == null) {
            tableDataInfo = getDataTable(null);
            tableDataInfo.setMsg("房间信息不存在！");
            return tableDataInfo;
        }

        //获取客户信息
        CustomInfo customInfo = getCustomInfo(room.getCommunityId());
        if (customInfo == null) {
            tableDataInfo = getDataTable(null);
            tableDataInfo.setMsg("未绑定房屋信息！请先绑定");
            return tableDataInfo;
        }
        // 获取登录人ID
        Repairs repairs = new Repairs();
        repairs.setRepairsPerson(customInfo.getCustomName());
        return getDataTable(repairsService.selectRepairsList(repairs));
    }


}
